//
//  HWRequestLogManage
//
//  Created by 李含文 on 2021/4/28.
//  Copyright © 2021 MINISO. All rights reserved.
//

#import "HWLogContentViewController.h"

@interface HWLogContentViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn_1;
@property (weak, nonatomic) IBOutlet UIButton *btn_2;
@property (weak, nonatomic) IBOutlet UIButton *btn_3;

@property (weak, nonatomic) IBOutlet UITextView *tv;
@property (weak, nonatomic) IBOutlet UIView *bgView;

/// <#name#>
@property (nonatomic, strong) NSArray *btnArray;

@end

@implementation HWLogContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.btnArray = @[self.btn_1, self.btn_2, self.btn_3];
    [self setUI];
}
- (void)setUI {
    self.title = @"请求详情";
    [self btnClick:self.btn_1];
    self.btn_1.cornerRadius = 5;
    self.btn_1.clipsToBounds = YES;
    self.btn_2.cornerRadius = 5;
    self.btn_2.clipsToBounds = YES;
    self.btn_3.cornerRadius = 5;
    self.btn_2.clipsToBounds = YES;
    self.bgView.cornerRadius = 5;
    self.bgView.clipsToBounds = YES;
    self.tv.editable = NO;
}
- (IBAction)btnClick:(UIButton *)btn {
    [self setBtnType:btn.tag];
    [self setContent:btn.tag];
}

- (void)setBtnType:(NSInteger)tag {
    for (int i = 0; i < self.btnArray.count; i++) {
        UIButton *btn = self.btnArray[i];
        if (tag-100==i) {
            [btn setTitleColor:UIColor.redColor forState:(UIControlStateNormal)];
            btn.backgroundColor = UIColor.whiteColor;
        } else {
            [btn setTitleColor:UIColor.blackColor forState:(UIControlStateNormal)];
            btn.backgroundColor = UIColor.clearColor;
        }
    }
}

- (void)setContent:(NSInteger)tag {
    NSString *log = @"";
    if (tag == 100) {
        NSMutableString *content = [NSMutableString string];
        [content appendString:@"curl -X "];
        [content appendString:self.model.HTTPMethod];
        [self.model.request.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
            [content appendString:[NSString stringWithFormat:@" -H \"%@:%@\"", key, obj]];
        }];
        
        if (self.model.body.length) {
            [content appendString:[NSString stringWithFormat:@" -d '%@'", self.model.body]];
        }
        
        [content appendString:[NSString stringWithFormat:@" \"%@\"", self.model.url]];
        log = [NSString stringWithFormat:@"请求信息:\n\n%@\n\n请求方式:\n%@\n\n请求路径:\n%@\n\n请求头:\n%@\n\n请求参数:\n%@\n\n返回数据:\n%@", content, self.model.HTTPMethod, self.model.url, self.model.header, self.model.body, self.model.content];
    } else if (tag == 101) {
        log = [NSString stringWithFormat:@"请求方式:\n%@\n\n请求路径:\n%@\n\n请求头:\n%@\n\n请求参数:\n%@", self.model.HTTPMethod, self.model.url, self.model.header, self.model.body];
    } else {
        log = [NSString stringWithFormat:@"返回数据:\n%@", self.model.content];
    }
    self.tv.text = log;
}
@end
